home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / TREEWN16.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  542 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.17  $
  6. //
  7. // Definition TTreeWindow class for Win16
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_TREEWN16_H)
  10. #define OWL_TREEWN16_H
  11.  
  12. #if defined(BI_PLAT_WIN32)
  13. # error Use #include <owl/treewind.h> for Win32 instead.
  14. #endif
  15. #if !defined(OWL_LISTBOX_H)
  16. # include <owl/listbox.h>
  17. #endif
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace OWL {
  21. #endif
  22.  
  23. // Generic definitions/compiler options (eg. alignment) preceeding the
  24. // definition of classes
  25. #include <services/preclass.h>
  26.  
  27. class _OWLCLASS TTreeItem;
  28. class _OWLCLASS TTreeNode;
  29. class _OWLCLASS TTreeLink;
  30. class _OWLCLASS TTreeWindow;
  31.  
  32. //
  33. // class TTreeItem
  34. // ~~~~~ ~~~~~~~~~
  35. // Contains the information about the item.
  36. //
  37. class _OWLCLASS TTreeItem {
  38.   public:
  39.     // Constructor
  40.     //
  41.     TTreeItem();
  42.     TTreeItem(const TTreeItem&);
  43.     TTreeItem(char far*, int len = 0);
  44.    ~TTreeItem();
  45.  
  46.     // Manipulate the text properties
  47.     //
  48.     void   SetText(const char far*, int len = 0);
  49.     void   GetText(char far*, int len = 0) const;
  50.  
  51.     // Manipulate the extra data
  52.     //
  53.     void   SetItemData(uint32 data);
  54.     uint32 GetItemData(void) const;
  55.  
  56.     TTreeItem operator =(const TTreeItem&);
  57.  
  58.   private:
  59.     void Init();
  60.  
  61.     char far* Text;
  62.     int       Length;
  63.     uint32    ItemData;
  64.  
  65.   friend class TTreeLink;
  66.   friend class TTreeWindow;
  67.   friend class TTreeNode;
  68. };
  69.  
  70.  
  71. //
  72. // class TTreeLink
  73. // ~~~~~ ~~~~~~~~~
  74. // This is a private class.
  75. //
  76. // This class contains the pointers its relatives.
  77. // It is used to maintain the information normally kept by the
  78. // common control.
  79. //
  80. class _OWLCLASS TTreeLink {
  81.   private:
  82.     TTreeLink(TTreeWindow& window);
  83.    ~TTreeLink();
  84.  
  85.     TTreeLink* AddAtHead(const TTreeItem& itemToAdd);
  86.     TTreeLink* AddAtTail(const TTreeItem& itemToAdd);
  87.     TTreeLink* AddAfter(const TTreeItem& itemToAdd);
  88.     TTreeLink* AddFirstChild(const TTreeItem& itemToAdd);
  89.     TTreeLink* AddLastChild(const TTreeItem& itemToAdd);
  90.     bool Delete();
  91.  
  92.     void GetSize(TSize& size, TFont* font);
  93.  
  94.     // Query status
  95.     //
  96.     bool IsRoot();
  97.     bool IsParent();
  98.     bool IsLastChild();
  99.     bool IsFirstChild();
  100.     bool IsOpened();
  101.     void ToggleOpened();
  102.  
  103.     // Indent level
  104.     //
  105.     int  GetIndentLevel();
  106.     void SetIndentLevel(int indentLevel);
  107.  
  108.   private:
  109.     bool Opened;
  110.     int  IndentLevel;
  111.  
  112.     TTreeItem*   TreeItem;
  113.     TTreeWindow& Window;
  114.  
  115.     // Links
  116.     //
  117.     TTreeLink* ParentLink;
  118.     TTreeLink* FirstChild;
  119.     TTreeLink* LastChild;
  120.     TTreeLink* NextSibling;
  121.     TTreeLink* PrevSibling;
  122.  
  123.   friend class TTreeNode;
  124.   friend class TTreeWindow;
  125. };
  126.  
  127.  
  128. //
  129. // class TTreeNode
  130. // ~~~~~ ~~~~~~~~~
  131. // Use this class to navigate the TTreeWindow.
  132. // Each node conceptually contains a pointer to a TTreeItem.
  133. //
  134. class _OWLCLASS TTreeNode {
  135.   public:
  136.  
  137. #pragma warn -cln               // constant is long
  138.     enum THowToInsert {
  139.       First = TVI_FIRST,
  140.       Last  = TVI_LAST,
  141.       Sort  = TVI_SORT
  142.     };
  143. #pragma warn .cln
  144.  
  145.     // Constructors and destructor
  146.     //
  147.     TTreeNode(void) : TreeView(0) {}
  148.     TTreeNode(TTreeWindow& tree, char far* text);
  149.     TTreeNode(TTreeWindow& tree);
  150.     TTreeNode(const TTreeNode&);
  151.     TTreeNode(const TTreeNode&, uint32);
  152.     TTreeNode(TTreeLink&);
  153.  
  154.     // The next two constructors are for creating new nodes.  Both create only
  155.     // the C++ TTreeNode object.  Call SetItem to make the new node appear in
  156.     // the treeview control.
  157.     //
  158.     TTreeNode(TTreeWindow& tw, const char far* text);
  159.     TTreeNode(TTreeWindow& tw, const char far* text, int index, int selIndex);
  160.  
  161.     // The next two constructors create a node object to represent a node
  162.     // that already exists.
  163.  
  164.     // Use this constructor in event handlers. The event message carries
  165.     // a TTwNotify structure.
  166.     //
  167.     TTreeNode(TTreeWindow& tw, TV_ITEM item);
  168.  
  169.     // This constructor is useful for enumerating nodes on a tree.
  170.     // Passing just the first parameter creates the node object for
  171.     // the tree's root, a good starting point.
  172.     //
  173. #pragma warn -cln               // constant is long
  174.     TTreeNode(TTreeWindow& tw, HTREEITEM hItem = TVI_ROOT);
  175. #pragma warn .cln
  176.  
  177.     // Tell the node which TTreeWindow it belongs to. Used with the default
  178.     // constructor.
  179.     //
  180.     void AttachToTree(TTreeWindow* tw);
  181.  
  182.    ~TTreeNode();
  183.  
  184.     // Navigation
  185.     //
  186.     TTreeNode GetParent() const;
  187.     TTreeNode GetChild() const;
  188.     TTreeNode GetNextSibling() const;
  189.     TTreeNode GetPrevSibling() const;
  190.     TTreeNode GetNextItem(uint32 flag) const;
  191.  
  192.     // Adding and deleting items from the tree
  193.     //
  194.     TTreeNode AddChild(const TTreeNode& node) const;
  195.     TTreeNode AddSibling(const TTreeNode& node) const;
  196.     TTreeNode InsertChild(const TTreeNode& node, uint32 how) const;
  197.     TTreeNode InsertItem(const TTreeNode& node) const;
  198.     bool      Delete();
  199.  
  200.     // These older add/delete methods use TTreeItem.  They have
  201.     // been replaced by the versions that operate on the node
  202.     // itself.  They are retained for backward compatibility.
  203.     //
  204.     TTreeNode AddChild(const TTreeItem&) const;         // add item at end
  205.     TTreeNode AddSibling(const TTreeItem&) const;
  206.     TTreeNode InsertChild(const TTreeItem&, uint32) const;
  207.     TTreeNode InsertItem(const TTreeItem&) const;
  208.  
  209.     // Operations on the node
  210.     //
  211.     bool      ExpandItem(uint32 flag);
  212.  
  213.     // Set/GetText store the text in the node's internal buffer so the
  214.     // caller doesn't have to worry about string length and buffer
  215.     // ownership.
  216.     //
  217.     bool       SetText(const char far* text, bool sendNow = true);
  218.     bool       GetText(char far*& text, bool getNew = false);
  219.     char far*  GetText(bool getNew = false);
  220.  
  221.     // These operations send messages to the control.  The message delivery
  222.     // might fail, so they return bool.
  223.     //
  224.     bool       SetItemData(uint32 data, bool sendNow = true);
  225.     bool       GetItemData(uint32& data, bool getNew = true);
  226.  
  227.     // The Set/Get accessors for individual node attributes send a message
  228.     // to send data to or receive data from the treeview control.  You can
  229.     // minimize the number of messages sent by using the sendNow or getNew
  230.     // parameters to prevent the message from being sent.  Then follow up
  231.     // with a single call to SetItem or GetItem.  This packages all pending
  232.     // business in a single message call.
  233.     //
  234.     //    SetText("MyText", false);     // these two send no message
  235.     //    SetItemData(1, false);
  236.     //    SetItem();                    // one msg is sent with both attributes
  237.     //
  238.     bool       GetItem(void);
  239.     bool       SetItem(void);
  240.  
  241.     bool       GetItem(TTreeItem* item);    // preserved for compatibility
  242.  
  243.  
  244.     TTreeNode operator=(const TTreeNode&);
  245.  
  246.     operator bool() const;
  247.  
  248.     TTreeLink* Link;
  249.  
  250.     // Codes for TTreeNode (TExpandCode, TNextCode)
  251.     //
  252.     // Retained for compatiblity, but no longer recommended.
  253.     // Use the TVE_ and TVGN_ constants directly instead of
  254.     // these enums.
  255.  
  256.     enum TExpandCode {
  257.       Collapse      = TVE_COLLAPSE,
  258.       Expand        = TVE_EXPAND,
  259.       Toggle        = TVE_TOGGLE,
  260.       CollapseReset = TVE_COLLAPSERESET // ignored in 16-bit
  261.     };
  262.  
  263.     enum TNextCode {
  264.       Root     = TVGN_ROOT,
  265.       Next     = TVGN_NEXT,
  266.       Previous = TVGN_PREVIOUS,
  267.       Parent   = TVGN_PARENT,
  268.       Child    = TVGN_CHILD,
  269.       Caret    = TVGN_CARET,
  270.  
  271.       FirstVisible    = TVGN_FIRSTVISIBLE, // these 4 are ignore in 16-bit
  272.       NextVisible     = TVGN_NEXTVISIBLE,
  273.       PreviousVisible = TVGN_PREVIOUSVISIBLE,
  274.       DropHilite      = TVGN_DROPHILITE
  275.     };
  276.  
  277.   protected:
  278.     TTreeItem ItemStruct;             // contains a TV_ITEM with HTREEITEM
  279.     TTreeWindow* TreeView;            // wrapper for item of this tree
  280.  
  281.   private:
  282.     TAPointer<char> CacheText;        // buffer for node text
  283.     static uint NodeTextCacheSize;    // size of text caching buffer
  284.  
  285.     // Used by copy constructor and assignment operator
  286.     //
  287.     TTreeNode& TTreeNode::CopyNode(const TTreeNode& ti);
  288. };
  289.  
  290. //
  291. // class TTreeWindow
  292. // ~~~~~ ~~~~~~~~~~~
  293. // Simulates the common control.
  294. //
  295. // Inheritance from TListBox is merely an implementation convenience.
  296. // TTreeWindow is not meant to use the TListBox interface.
  297. //
  298. class _OWLCLASS TTreeWindow : public TListBox {
  299.   public:
  300.  
  301.     // Constructors and destructor
  302.     //
  303.     TTreeWindow(TWindow* parent, int id, int x, int y, int w, int h,
  304.                 uint32 style = 0, TModule* module = 0);
  305.     TTreeWindow(TWindow* parent, int resourceId, TModule* module = 0);
  306.    ~TTreeWindow();
  307.  
  308.     // Set the styles
  309.     //
  310.     void SetStyle(uint32 style);
  311.     bool HasStyle(uint32 style);
  312.  
  313.     // Return special nodes
  314.     //
  315.     TTreeNode GetRoot();
  316.     TTreeNode GetSelection();
  317.  
  318.     // Indent level
  319.     //
  320.     uint GetIndent();
  321.     void SetIndent(uint);
  322.  
  323.     TFont* GetFont();
  324.  
  325.     void Update();
  326.  
  327.     // enum retained for compatiblity, but no longer recommended.
  328.     // Use the TVS_* style constants directly instead of this enum.
  329.     //
  330.     enum TStyle {
  331.       twsNone       = 0x0000,
  332.       twsHasLines   = TVS_HASLINES,
  333.       twsHasButtons = TVS_LINESATROOT,
  334.  
  335.       twsEditLabels      = TVS_EDITLABELS,      // ignored in 16-bit
  336.       twsDisableDragDrop = TVS_DISABLEDRAGDROP, // ignored in 16-bit
  337.       twsShowSelAlways   = TVS_SHOWSELALWAYS    // ignored in 16-bit
  338.     };
  339.  
  340.   protected:
  341.     TTreeLink* GetLinkAtIndex(int index);
  342.     int        AddLinks(TTreeLink* parent, int indentLevel, int insertAfterIndex);
  343.     void       OpenLink(TTreeLink* link, int index);
  344.     void       CloseLink(TTreeLink* link, int index);
  345.  
  346.     // owner draw methods
  347.     //
  348.     void MeasureItem(MEASUREITEMSTRUCT far& measureItemInfo);
  349.     void ODAFocus(DRAWITEMSTRUCT far& drawInfo);
  350.     void ODASelect(DRAWITEMSTRUCT far& drawInfo);
  351.     void ODADrawEntire(DRAWITEMSTRUCT far& drawInfo);
  352.     void PaintLink(DRAWITEMSTRUCT far& drawInfo);
  353.  
  354.     void  EvChar(uint key, uint repeatCount, uint flags);
  355.     void  EvLDblClk();
  356.     void  EvSetFont(HFONT hFont, bool redraw);
  357.     HFONT EvGetFont();
  358.  
  359.     TFont* Font;
  360.     uint32 Style;
  361.  
  362.   private:
  363.     void Init();
  364.  
  365.     TTreeLink* RootLink;
  366.     uint       IndentUnits;
  367.  
  368.     // Hidden to prevent accidental copying or assignment
  369.     //
  370.     TTreeWindow(const TTreeWindow&);
  371.     TTreeWindow& operator=(const TTreeWindow&);
  372.  
  373.     // Used when painting
  374.     //
  375.     static const int ButtonHeight;
  376.     static const int ButtonWidth;
  377.  
  378.   friend class TTreeNode;
  379.  
  380.   DECLARE_RESPONSE_TABLE(TTreeWindow);
  381. };
  382.  
  383. // Generic definitions/compiler options (eg. alignment) following the
  384. // definition of classes
  385. #include <services/posclass.h>
  386.  
  387. #if defined(BI_NAMESPACE)
  388. } // namespace OWL
  389. #endif
  390.  
  391. //----------------------------------------------------------------------------
  392. // TTreeItem
  393. //
  394.  
  395. //
  396. inline void
  397. TTreeItem::SetItemData(uint32 data)
  398. {
  399.   ItemData = data;
  400. }
  401.  
  402. //
  403. inline uint32
  404. TTreeItem::GetItemData() const
  405. {
  406.   return ItemData;
  407. }
  408.  
  409.  
  410. //----------------------------------------------------------------------------
  411. // TTreeLink
  412. //
  413.  
  414. //
  415. inline bool
  416. TTreeLink::IsRoot()
  417. {
  418.   return ParentLink == 0;
  419. }
  420.  
  421. //
  422. inline bool
  423. TTreeLink::IsOpened()
  424. {
  425.   return Opened;
  426. }
  427.  
  428. //
  429. inline int
  430. TTreeLink::GetIndentLevel()
  431. {
  432.   return IndentLevel;
  433. }
  434.  
  435. //
  436. inline void
  437. TTreeLink::SetIndentLevel(int indentLevel)
  438. {
  439.   IndentLevel = indentLevel;
  440. }
  441.  
  442. //
  443. inline bool
  444. TTreeLink::IsParent()
  445. {
  446.   return FirstChild != 0 && LastChild != 0;
  447. }
  448.  
  449.  
  450. //----------------------------------------------------------------------------
  451. // TTreeNode
  452. //
  453.  
  454. //
  455. inline TTreeNode
  456. TTreeNode::GetParent() const
  457. {
  458.   return GetNextItem(Parent);
  459. }
  460.  
  461. //
  462. inline TTreeNode
  463. TTreeNode::GetChild() const
  464. {
  465.   return GetNextItem(Child);
  466. }
  467.  
  468. //
  469. inline TTreeNode
  470. TTreeNode::GetNextSibling() const
  471. {
  472.   return GetNextItem(Next);
  473. }
  474.  
  475. //
  476. inline TTreeNode
  477. TTreeNode::GetPrevSibling() const
  478. {
  479.   return GetNextItem(Previous);
  480. }
  481.  
  482. //
  483. inline TTreeNode
  484. TTreeNode::AddChild(const TTreeItem& item) const
  485. {
  486.   return TTreeNode(*Link->AddLastChild(item));
  487. }
  488.  
  489. //
  490. inline TTreeNode
  491. TTreeNode::AddChild(const TTreeNode& node) const
  492. {
  493.   return TTreeNode(*Link->AddLastChild(node.ItemStruct));
  494. }
  495.  
  496. //
  497. inline TTreeNode
  498. TTreeNode::AddSibling(const TTreeNode& node) const
  499. {
  500.   return TTreeNode(*Link->AddAtTail(node.ItemStruct));
  501. }
  502.  
  503. inline TTreeNode
  504. TTreeNode::InsertChild(const TTreeNode& node, uint32 how) const
  505. {
  506.   return InsertChild(node.ItemStruct, how);
  507. }
  508.  
  509. inline TTreeNode
  510. TTreeNode::InsertItem(const TTreeNode& node) const
  511. {
  512.   return TTreeNode(*(Link->AddAfter(node.ItemStruct)));
  513. }
  514.  
  515. //----------------------------------------------------------------------------
  516. // TTreeWindow
  517. //
  518.  
  519. //
  520. inline TTreeNode
  521. TTreeWindow::GetRoot()
  522. {
  523.   return TTreeNode(*RootLink);
  524. }
  525.  
  526. //
  527. inline uint
  528. TTreeWindow::GetIndent()
  529. {
  530.   return IndentUnits;
  531. }
  532.  
  533. //
  534. inline TFont*
  535. TTreeWindow::GetFont()
  536. {
  537.   return Font;
  538. }
  539.  
  540. #endif  // OWL_TREEWN16_H
  541.  
  542.